package com.banciyuan.bcywebview.biz.main.discover.top;

import android.content.Context;

import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.api.CoserApi;
import com.banciyuan.bcywebview.api.IllustApi;
import com.banciyuan.bcywebview.api.NovelApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.myinterface.HelpActionInterface;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import com.banciyuan.bcywebview.utils.http.BcyPair;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.ActiveUser;

public class TopListHelper {

    private Context mContext;
    private HelpActionInterface mInterface;
    private RequestQueue mQueue;
    private List<ActiveUser> mList = new ArrayList<>();
    private int pagenum = 1;
    private int interface_type = 0;
    private String action;

    public TopListHelper(Context mContext, HelpActionInterface mInterface) {
        this.mContext = mContext;
        this.mInterface = mInterface;
        mQueue = VolleyQueue.getRquest(this.mContext);
    }

    public void getFrontListData(String action) {
        this.action = action;
        pagenum = 1;
        interface_type = 0;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                dealListJson(response);
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mInterface.onNetError("");
            }
        };
        List<BcyPair> paramsList = new ArrayList<>();
        paramsList.add(new BcyPair(HttpUtils.INDEX_P, pagenum + ""));
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        paramsList.add(new BcyPair(HttpUtils.FACE, "f"));
        Map<String, String> map = HttpUtils.getData(paramsList);
        String url = HttpUtils.BASE_URL + action;
        OnErrorProxy errorProxy = new OnErrorProxy(onError, onSuccess, url, mContext, map);
        StringRequestParam getUserdataRequest = new StringRequestParam(Method.POST, url, map, onSuccess, errorProxy);
        mQueue.add(getUserdataRequest);
    }

    public void getLastListData(String action) {

        pagenum += 1;
        interface_type = 1;
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                dealListJson(response);
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                pagenum -= 1;
            }
        };
        if (pagenum > 5) return;
        List<BcyPair> paramsList = new ArrayList<BcyPair>();
        //todo
        paramsList.add(new BcyPair(HttpUtils.INDEX_P, pagenum + ""));
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        paramsList.add(new BcyPair(HttpUtils.FACE, "f"));
        Map<String, String> map = HttpUtils.getData(paramsList);
        String url = HttpUtils.BASE_URL + action;
        OnErrorProxy errorProxy = new OnErrorProxy(onError, onSuccess, url, mContext, map);
        StringRequestParam getUserdataRequest = new StringRequestParam(Method.POST, url, map, onSuccess, errorProxy);
        mQueue.add(getUserdataRequest);
    }

    private void dealListJson(String jsonResult) {
        try {
            if (StatusCodeUtils.dealStatusCode(jsonResult, mContext)) {
                Gson gson = new Gson();
                JSONObject jsonObject = new JSONObject(jsonResult);
                JSONArray result = jsonObject.getJSONArray(HttpUtils.UPLOAD_DATA);
                List<ActiveUser> templist = gson.fromJson(result.toString(), new TypeToken<List<ActiveUser>>() {
                }.getType());
                setContent(templist);
                if ((templist == null || templist.isEmpty())) {
                    mInterface.nodataError("");
                } else {
                    if (interface_type == 0) mList.clear();
                    mList.addAll(templist);
                    getDataSuccess();
                }

            } else {
                mInterface.onNetError("");
            }
        } catch (Exception e) {
            e.printStackTrace();
            getDataSuccess();
        }
    }

    private void getDataSuccess() {
        if (interface_type == 0) mInterface.getFrontDataSuccess("");
        else mInterface.getLastDataSuccess("");
    }

    public List<ActiveUser> getShowList() {
        return mList;
    }

    private void setContent(List<ActiveUser> templist) {
        for (ActiveUser a : templist) {
            if (action.equals(CoserApi.getTopcoser())) {
                a.setType("coser");
            } else if (action.equals(IllustApi.getTopdrawer())) {
                a.setType("drawer");
            } else if (action.equals(NovelApi.getTopwriter())) {
                a.setType("writer");
            } else {
                a.setType("user");
            }
            a.setPost_count(a.getPost_num());
        }
    }

}
